package jsf.test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ApplicationScoped;
import jsf.test.Vulnerability;

/**
 *
 * @author t41507
 * @version 26.05.2014
 */
@ManagedBean
@ApplicationScoped
public class VulnerabilityService implements java.io.Serializable {

    private static final Logger LOG = Logger.getLogger(VulnerabilityService.class.getName());

    private final static String[] names;
    private final static String[] scores;

    static {
        names = new String[10];
        names[0] = "CVE-2014-0001";
        names[1] = "CVE-2014-0002";
        names[2] = "CVE-2014-0003";
        names[3] = "CVE-2014-0004";
        names[4] = "CVE-2014-0005";
        names[5] = "CVE-2014-0006";
        names[6] = "CVE-2014-0007";
        names[7] = "CVE-2014-0008";
        names[8] = "CVE-2014-0009";
        names[9] = "CVE-2014-0010";
        scores = new String[10];
        scores[0] = "10.0";
        scores[1] = "9.0";
        scores[2] = "8.0";
        scores[3] = "7.0";
        scores[4] = "6.0";
        scores[5] = "5.0";
        scores[6] = "4.0";
        scores[7] = "3.0";
        scores[8] = "2.0";
        scores[9] = "1.0";
    }

    public VulnerabilityService() {
    }

    public List<Vulnerability> createList(int size) {
        List<Vulnerability> list = new ArrayList<>();
        for (int i = 0; i < size; i++) {
            Vulnerability temp = new Vulnerability();
            if (size < 10) {
                temp.setName("CVE-2014-000" + (i + 1));
            } else {
                temp.setName("CVE-2014-00" + (i + 1));
            }
            temp.setScore(getRandomScore());
            temp.setPublished(new Date());
            list.add(temp);
        }
        return list;
    }

    public String getRandomName() {
        return names[(int) (Math.random() * 10)];
    }

    public List<String> getNames() {
        return Arrays.asList(names);
    }

    public List<String> getScores() {
        return Arrays.asList(scores);
    }

    private String getRandomScore() {
        return scores[(int) (Math.random() * 10)];
    }
}
